package com.xinweisi.dataserver.module.memberdata.tradeflow;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinweisi.dataserver.common.checkDBName.CheckDBNameService;
import com.xinweisi.dataserver.common.domain.PageResultDTO;
import com.xinweisi.dataserver.common.domain.ResponseDTO;
import com.xinweisi.dataserver.module.memberdata.tradeflow.domain.dto.MemberTradeFlowQueryDTO;
import com.xinweisi.dataserver.module.memberdata.tradeflow.domain.entity.MemberTradeFlowEntity;
import com.xinweisi.dataserver.module.memberdata.tradeflow.domain.vo.MemberTradeFlowResultVO;
import com.xinweisi.dataserver.module.memberdata.tradeflow.domain.vo.MemberTradeFlowVO;
import com.xinweisi.dataserver.module.memberdata.user.domain.entity.UserEntity;
import com.xinweisi.dataserver.third.RedisService;
import com.xinweisi.dataserver.util.DateUtil;
import com.xinweisi.dataserver.util.SmartPageUtil;
import com.xinweisi.dataserver.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * MemberTradeFlowService
 *
 * @author lijie1
 * @version 1.0
 * @date 2020/7/21 0021 14:34
 */
@Slf4j
@Service
public class MemberTradeFlowService {
    @Autowired
    private MemberTradeFlowDao memberTradeFlowDao;

    private RedisService redisService;
    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    @Autowired
    private CheckDBNameService checkDBNameService;

    /**
     * 查询会员的交易流水
     * @param queryDTO
     * @author lijie1
     * @date 2020/7/21 0021 14:47
     * @return com.xinweisi.dataserver.common.domain.ResponseDTO<com.xinweisi.dataserver.common.domain.PageResultDTO < com.xinweisi.dataserver.module.memberdata.tradeflow.domain.vo.MemberTradeFlowVO>>
     */
    public ResponseDTO<MemberTradeFlowResultVO> getMemberTradeFlow(MemberTradeFlowQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<MemberTradeFlowVO> entityList = new ArrayList<>();
        Double betsumAmount = 0.0;
        Double paiprizeAmount = 0.0;
        Double yinkui = 0.0;
        for (int i=0;i<queryDTO.getDBNameList().size();i++){
            String DBName = queryDTO.getDBNameList().get(i);//数据库名称
            queryDTO.setDBName(DBName);
            int flag = checkDBNameService.checkDBName(queryDTO.getDBName());
            if(flag==0){
                continue;
            }
            String nowDBName = "daylog_"+DateUtil.getDate(new Date());
            if(nowDBName.equals(DBName)){
                List<MemberTradeFlowEntity> list = memberTradeFlowDao.selectMemberTradeFlowList(queryDTO);
                for (MemberTradeFlowEntity obj:list ) {
                    MemberTradeFlowVO vo = new MemberTradeFlowVO();
                    UserEntity en = UserUtil.userMap.get(obj.getUserId());//获取交易商ByUserId
                    vo.setAccount(obj.getUserId());//账号
                    if(en!=null){
                        vo.setAccountName(en.getAccount());//账号名称
                    }else {
                        vo.setAccountName("");//账号名称
                    }
                    vo.setGameType(obj.getGameType());//游戏类型
                    vo.setWagerTime(obj.getGameTime());//下注时间
                    String changeV = obj.getBalanceChange();
                    String[] balanceS = changeV.split("_");
                    double amountA = Double.parseDouble(balanceS[1])-Double.parseDouble(balanceS[0]);
                    double amountT = 0.0;
                    if(balanceS.length==3){
                        amountT = Double.parseDouble(balanceS[2]);
                    }
                    vo.setResult(Double.toString(amountA));//投注结果
                    if(amountA>=0){
                        paiprizeAmount = paiprizeAmount + amountA;
                    }
                    vo.setAmount(Double.toString(amountT/obj.getMultiple()));//投注金额
                    betsumAmount = betsumAmount+ amountT/obj.getMultiple();
                    yinkui = yinkui + amountA;
                    vo.setOrderId(obj.getOrderId());
                    entityList.add(vo);
                }
            }else {
                List<Object> list = redisService.lGet("TradeFlow"+DBName+queryDTO.getUserId(),0,-1);
                if(list==null || list.size()==0){
                    List<MemberTradeFlowEntity> list1 = (memberTradeFlowDao.selectMemberTradeFlowList(queryDTO));
                    for (MemberTradeFlowEntity obj:list1 ) {
                        MemberTradeFlowVO vo = new MemberTradeFlowVO();
                        UserEntity en = UserUtil.userMap.get(obj.getUserId());//获取交易商ByUserId
                        vo.setAccount(obj.getUserId());//账号
                        if(en!=null){
                            vo.setAccountName(en.getAccount());//账号名称
                        }else {
                            vo.setAccountName("");//账号名称
                        }
                        vo.setGameType(obj.getGameType());//游戏类型
                        vo.setWagerTime(obj.getGameTime());//下注时间
                        String changeV = obj.getBalanceChange();
                        String[] balanceS = changeV.split("_");
                        double amountA = Double.parseDouble(balanceS[1])-Double.parseDouble(balanceS[0]);
                        double amountT = 0.0;
                        if(balanceS.length==3){
                            amountT = Double.parseDouble(balanceS[2]);
                        }
                        vo.setResult(Double.toString(amountA));
                        if(amountA>=0){
                            paiprizeAmount = paiprizeAmount + amountA;
                        }
                        vo.setAmount(Double.toString(amountT/obj.getMultiple()));
                        betsumAmount = betsumAmount+ amountT/obj.getMultiple();
                        yinkui = yinkui + amountA;
                        vo.setOrderId(obj.getOrderId());
                        entityList.add(vo);
                    }
                    if(list1.size()!=0){redisService.lSet("TradeFlow"+DBName+queryDTO.getUserId(),list1);}
                }else {
                    for (Object obj:list) {
                        List<MemberTradeFlowVO> list2 = (List<MemberTradeFlowVO>) obj;
                        for (MemberTradeFlowVO entity:list2 ) {
                            betsumAmount = betsumAmount+ Double.parseDouble(entity.getAmount());
                            paiprizeAmount = paiprizeAmount + Double.parseDouble(entity.getResult());
                            entityList.add(entity);
                        }
                    }
                }
            }
        }
        page.setTotal(entityList.size());
        PageResultDTO<MemberTradeFlowVO> pageResultDTO = SmartPageUtil.convert2PageResult(page, SmartPageUtil.startPage(entityList,queryDTO.getPageNum(),queryDTO.getPageSize()), MemberTradeFlowVO.class);
        MemberTradeFlowResultVO entityResult = new MemberTradeFlowResultVO();
        entityResult.setDataList(pageResultDTO);
        entityResult.setBetCount((long)entityList.size());
        entityResult.setBetsumAmount(betsumAmount);
        if(paiprizeAmount>0){
            entityResult.setPaiprizeAmount(paiprizeAmount);
        }else {
            entityResult.setPaiprizeAmount(0.0);
        }
        entityResult.setYinkui(yinkui);
        return ResponseDTO.succData(entityResult);
    }

}
